package com.dzh.controller;

import com.dzh.mapper.QppriorityMapper;
import com.dzh.mapper.TicketMapper;
import com.dzh.mapper.UserTicketMapper;
import com.dzh.pojo.Qppriority;
import com.dzh.pojo.Ticket;
import com.dzh.pojo.UserTicket;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Date;


@Controller
public class TicketController {
    @Autowired
    TicketMapper ticketMapper;
    @Autowired
    UserTicketMapper userTicketMapper;
    @Autowired
    QppriorityMapper qppriorityMapper;
    @RequestMapping("/goupiaolist")
    public String togoupiaolist(Model model, String chufadi, String mudidi,HttpSession session){
        List<Ticket>  tickets = ticketMapper.queryTicketByad(chufadi,mudidi);
        model.addAttribute("newtickets",tickets);
        return "bancixinxi_list_gukebuy";
    }


    @RequestMapping("/gukebancilist")
    public String gukebancilist(Model model, HttpSession session,HttpServletResponse response) throws IOException {
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        PrintWriter out=response.getWriter();
      String username =  (String)session.getAttribute("loginUser");
      //如果没买票的话就返回index界面
        int o = userTicketMapper.queryUsernameifnull(username);
        if(o==0){
            out.println("<script>alert('你还未购买任何票');</script>");
            return "index";
        }
        //先从购票的数据库把tid取出来
         List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);

       //一个一个循环把ticket放入userticket中
        for(UserTicket userTicket:userTickets){
            Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

            userTicket.setTicket(ticket);
        }
        model.addAttribute("userTicketlist",userTickets);
         return "bancixinxi_list_gukeguanli";
    }

    @RequestMapping("/gukebuyticket")
    public String gukebuyticket(Model model, HttpSession session, String tid, String chufadi, String mudidi, HttpServletResponse response) throws IOException {
        String username =  (String)session.getAttribute("loginUser");
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        PrintWriter out=response.getWriter();
        if(username==null){
            out.println("<script>alert('请先登录');</script>");
            return "login";
        }

        //看看余票是否为0张了
     int o =   ticketMapper.queryTicketYupiao(tid);
     if(o==0){
         List<Ticket>  tickets = ticketMapper.queryTicketByad(chufadi,mudidi);
         model.addAttribute("newtickets",tickets);


          //抢票
      int p =   qppriorityMapper.queryfirstpriority(tid);
         if(p==0){
             qppriorityMapper.addfirstpriority(username,tid);
             qppriorityMapper.queryQiangpiaolist(username);
          List<Qppriority> qppriorityList = qppriorityMapper.queryQiangpiaolist(username);
             for(Qppriority qppriority:qppriorityList){
                 Ticket ticket = ticketMapper.queryAllTicketByTid(qppriority.getTid());

                 qppriority.setTicket(ticket);
             }
             model.addAttribute("userTicketlistqiangpiao",qppriorityList);
         }


         else{
            int bigpriority = qppriorityMapper.querypBigriority(tid);
             qppriorityMapper.addpriority(username,tid,bigpriority);
             qppriorityMapper.queryQiangpiaolist(username);
             List<Qppriority> qppriorityList = qppriorityMapper.queryQiangpiaolist(username);
             for(Qppriority qppriority:qppriorityList){
                 Ticket ticket = ticketMapper.queryAllTicketByTid(qppriority.getTid());

                 qppriority.setTicket(ticket);
             }
             model.addAttribute("userTicketlistqiangpiao",qppriorityList);

         }




         out.println("<script>alert('没票了，正在帮你抢票！！！');</script>");
         return "bancixinxi_list_qiangpiao";
     }
    else{
         userTicketMapper.addUserTicket(username,tid);
        ticketMapper.updateTicketBySale(tid,o);
     }
        out.println("<script>alert('购买成功！！！');</script>");
        return "index";
    }

    @RequestMapping("/returnticket")
    public String returnticket(Model model,String tid,int yupiao,HttpSession session,HttpServletResponse response) throws IOException {
        String username =  (String)session.getAttribute("loginUser");
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        //退牌后有人抢票
        int p =   qppriorityMapper.queryfirstpriority(tid);
        //没人抢票
        if(p==0){
            ticketMapper.deleteTicketByReturn(username,tid);
            ticketMapper.updateTicketByReturn(tid,yupiao);
        }
        //有人抢票
        else{
            ticketMapper.deleteTicketByReturn(username,tid);
          Qppriority qppriority =  qppriorityMapper.queryprioritymin(tid);
          String username1 = qppriority.getUsername();
          int priority1 = qppriority.getPriority();
            userTicketMapper.addUserTicket(username1,tid);
            qppriorityMapper.deletePriority(priority1,tid);
        }




        int o = userTicketMapper.queryUsernameifnull(username);
        if(o==0){PrintWriter out=response.getWriter();
            out.println("<script>alert('你的票程现在空啦');</script>");
            return "index";
        }
        //先从购票的数据库把tid取出来
        List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);

        //一个一个循环把ticket放入userticket中
        for(UserTicket userTicket:userTickets){
            Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

            userTicket.setTicket(ticket);
        }

            model.addAttribute("userTicketlist",userTickets);
            return "bancixinxi_list_gukeguanli";

    }

    @RequestMapping("/togaiqian")
    public String togaiqian(Model model,String tid,int yupiao,String chufadi,String mudidi,String type,HttpSession session){
        List<Ticket> tickets = ticketMapper.queryTicketByGaiqian(chufadi,mudidi,type);
        session.setAttribute("gaiqiantid",tid);
        session.setAttribute("gaiqianyupiao",yupiao);
        model.addAttribute("newtickets",tickets);
        return  "bancixinxi_list_gukegaiqian";
    }

    @RequestMapping("/gaiqian")
    public String gaiqian(Model model,HttpSession session,String tid,int yupiao,HttpServletResponse response) throws IOException {
        String username = (String) session.getAttribute("loginUser");
        response.setContentType("text/html;utf8");
        response.setCharacterEncoding("utf8");
        String gaiqiantid = (String) session.getAttribute("gaiqiantid");
        int gaiqianyupiao = (int) session.getAttribute("gaiqianyupiao");



        //看改签这张票是否为0
        if (yupiao == 0) {
            PrintWriter out = response.getWriter();
            out.println("<script>alert('改签的票没票啦！！正在帮你抢票');</script>");
            int qpyupiao = qppriorityMapper.queryfirstpriority(tid);
            //改签的这张票为0，说明要么有人抢要么没人抢
            if (qpyupiao == 0) {
                qppriorityMapper.addfirstpriority(username, tid);
            } else {
                //有人抢了
                int bigpriority = qppriorityMapper.querypBigriority(tid);
                qppriorityMapper.addpriority(username, tid, bigpriority);
            }
            //把你原有的票删掉
            ticketMapper.deleteTicketByReturn(username, gaiqiantid);
            //看改签前的票有没有人抢
            int qpgaiqian = qppriorityMapper.queryfirstpriority(gaiqiantid);
            int gaiqianqian = ticketMapper.queryTicketYupiao(gaiqiantid);
            //要么有人抢要么没人抢
            if (gaiqianqian == 0) {
                //没人抢
                if (qpgaiqian == 0) {
                    ticketMapper.updateTicketByReturn(gaiqiantid, gaiqianyupiao);

//                    int o =      qppriorityMapper.queryfirstpriority(tid);
//                    int bigpriority = qppriorityMapper.querypBigriority(tid);
//                    if(o==0){
//                        qppriorityMapper.addfirstpriority(username,tid);
//                    }
//                    else{
//                        qppriorityMapper.addpriority(username,tid,bigpriority);
//                    }


                    List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);
                    //一个一个循环把ticket放入userticket中
                    for (UserTicket userTicket : userTickets) {
                        Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

                        userTicket.setTicket(ticket);
                    }
                    model.addAttribute("userTicketlist", userTickets);
                    return "bancixinxi_list_gukeguanli";

                }
                //有人抢，票一定为0
                else{
                    Qppriority qppriority = qppriorityMapper.queryprioritymin(gaiqiantid);
                    int priority = qppriority.getPriority();
                    String usernameqiangpiaoren = qppriority.getUsername();
                    qppriorityMapper.deletePriority(priority, gaiqiantid);
                    userTicketMapper.addUserTicket(usernameqiangpiaoren,gaiqiantid);


//                    int o =      qppriorityMapper.queryfirstpriority(tid);
//                    int bigpriority = qppriorityMapper.querypBigriority(tid);
//                    if(o==0){
//                        qppriorityMapper.addfirstpriority(username,tid);
//                    }
//                    else{
//                        qppriorityMapper.addpriority(username,tid,bigpriority);
//                    }

                    List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);

                    //一个一个循环把ticket放入userticket中
                    for (UserTicket userTicket : userTickets) {
                        Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

                        userTicket.setTicket(ticket);
                    }
                    model.addAttribute("userTicketlist", userTickets);
                    return "bancixinxi_list_gukeguanli";
                }




            }
            //改签前那张票不为0，没人抢
            else {
                ticketMapper.updateTicketByReturn(gaiqiantid, gaiqianyupiao);


//          int o =      qppriorityMapper.queryfirstpriority(tid);
//         int bigpriority = qppriorityMapper.querypBigriority(tid);
//          if(o==0){
//              qppriorityMapper.addfirstpriority(username,tid);
//          }
//          else{
//              qppriorityMapper.addpriority(username,tid,bigpriority);
//          }


                List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);

                //一个一个循环把ticket放入userticket中
                for (UserTicket userTicket : userTickets) {
                    Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

                    userTicket.setTicket(ticket);
                }
                model.addAttribute("userTicketlist", userTickets);
                return "bancixinxi_list_gukeguanli";


            }

        }

        //改签的票不为0
        else {

            //把你原有的票删掉
            ticketMapper.deleteTicketByReturn(username, gaiqiantid);
            //看改签前的票有没有人抢
            int qpgaiqian = qppriorityMapper.queryfirstpriority(gaiqiantid);
            int gaiqianqian = ticketMapper.queryTicketYupiao(gaiqiantid);
            //要么有人抢要么没人抢
            if (gaiqianqian == 0) {
                //没人抢
                if (qpgaiqian == 0) {
                    ticketMapper.updateTicketByReturn(gaiqiantid, gaiqianyupiao);
                    ticketMapper.updateTicketBySale(tid,yupiao);
                    userTicketMapper.addUserTicket(username,tid);


                    List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);

                    //一个一个循环把ticket放入userticket中
                    for (UserTicket userTicket : userTickets) {
                        Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

                        userTicket.setTicket(ticket);
                    }
                    model.addAttribute("userTicketlist", userTickets);
                    return "bancixinxi_list_gukeguanli";

                }
                //有人抢，票一定为0
                else{
                    Qppriority qppriority = qppriorityMapper.queryprioritymin(gaiqiantid);
                    int priority = qppriority.getPriority();
                    String usernameqiangpiaoren = qppriority.getUsername();

                    qppriorityMapper.deletePriority(priority, gaiqiantid);
                    userTicketMapper.addUserTicket(usernameqiangpiaoren, gaiqiantid);
                    ticketMapper.updateTicketBySale(tid,yupiao);



                    List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);

                    //一个一个循环把ticket放入userticket中
                    for (UserTicket userTicket : userTickets) {
                        Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

                        userTicket.setTicket(ticket);
                    }
                    model.addAttribute("userTicketlist", userTickets);
                    return "bancixinxi_list_gukeguanli";
                }

            }
            //没人抢的话
            else{
                System.out.println(yupiao);
                System.out.println(gaiqianyupiao);
                ticketMapper.updateTicketBySale(tid,yupiao);
                ticketMapper.updateTicketByReturn(gaiqiantid,gaiqianyupiao);
                userTicketMapper.addUserTicket(username,tid);
                ticketMapper.deleteTicketByReturn(username,gaiqiantid);
                List<UserTicket> userTickets = userTicketMapper.queryAllByUsername(username);

                //一个一个循环把ticket放入userticket中
                for (UserTicket userTicket : userTickets) {
                    Ticket ticket = ticketMapper.queryAllTicketByTid(userTicket.getTid());

                    userTicket.setTicket(ticket);
                }
                model.addAttribute("userTicketlist", userTickets);
                return "bancixinxi_list_gukeguanli";
            }

        }




        }




    //抢票

    @RequestMapping("/toqiangpiao")
    public String toqiangpiao(Model model,HttpSession session){
        String username =  (String)session.getAttribute("loginUser");
        qppriorityMapper.queryQiangpiaolist(username);
        List<Qppriority> qppriorityList = qppriorityMapper.queryQiangpiaolist(username);
        for(Qppriority qppriority:qppriorityList){
            Ticket ticket = ticketMapper.queryAllTicketByTid(qppriority.getTid());

            qppriority.setTicket(ticket);
        }
        model.addAttribute("userTicketlistqiangpiao",qppriorityList);
        return "bancixinxi_list_qiangpiao";
    }








}
